﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace ParticleSystem
{
    public class Helper
    {
        public List<Texture2D> Images { set; get; }
        //needs have textures arrays or smth and then get them by ID (int)
        public Emitter emitter { set; get; }

        public SpriteFont Font;
        public string Message = "";
        public KeyboardState pks;

        private Vector2 Rotation_Center;

        private Rectangle Size_Rectangle;
        private Texture2D pixel;

        public Texture2D Background;
        public bool DrawBackground = false;
        public Vector2 BGPosition = Vector2.Zero;
        public bool changeBGPosition = false;
        GraphicsDeviceManager graphics;

        public void HelperInit(ContentManager content, int amount, GraphicsDeviceManager graphics)
        {
            emitter = new Emitter();
            emitter.Init(amount);
            emitter.Start();

            Images = new List<Texture2D>();
            Images.Add(content.Load<Texture2D>("star"));
            Images.Add(content.Load<Texture2D>("lazer"));

            Font = content.Load<SpriteFont>("Pericles8");

            Rotation_Center = new Vector2(Images[emitter.Image].Width/2, Images[emitter.Image].Height/2);
            Size_Rectangle = new Rectangle(emitter.Size_X, emitter.Size_Y, emitter.Width, emitter.Height);
            pixel = content.Load<Texture2D>("pixel");

            this.graphics = graphics;
            updateEmitterPosition(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
        }

        public void Update(GameTime gametime, MouseState MS)
        {
            if (changeBGPosition)
            {
                if (MS.LeftButton == ButtonState.Pressed)
                {
                    BGPosition = new Vector2(MS.X, MS.Y);
                }
            }

            emitter.Update((float)gametime.ElapsedGameTime.Milliseconds);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //new 2 lines draws background if needed
            if (DrawBackground)
                spriteBatch.Draw(Background, BGPosition, Color.White);
            
            
            for (int i = 0; i < emitter.Amount; i++)
            {
                if (emitter.parts[i].Alive)
                {
                    //spriteBatch.Draw(Images[0], new Vector2(emit.parts[i].Position_X, emit.parts[i].Position_Y), Color.White);
                    spriteBatch.Draw(
                        Images[emitter.Image],  //texture
                        new Vector2(emitter.parts[i].Position_X, emitter.parts[i].Position_Y), //position
                        null,   //source rectangle
                        Color.White, // color
                        emitter.parts[i].Rotation, //rotates particle
                        Rotation_Center, //Origin , is making it rotate from the center not upper left corner
                        emitter.parts[i].Scale, //scale
                        SpriteEffects.None, //effects
                        1f);    //layer depth
                }
            }
            if (emitter.useBoundingBox)
            {
                //Top and bottom
                spriteBatch.Draw(pixel, new Rectangle(Size_Rectangle.X, Size_Rectangle.Y, Size_Rectangle.Width, 2), Color.Red);
                spriteBatch.Draw(pixel, new Rectangle(Size_Rectangle.X, Size_Rectangle.Bottom, Size_Rectangle.Width, 2), Color.Red);
                //Left and Right
                spriteBatch.Draw(pixel, new Rectangle(Size_Rectangle.X, Size_Rectangle.Y, 2, Size_Rectangle.Height), Color.Red);
                spriteBatch.Draw(pixel, new Rectangle(Size_Rectangle.Right, Size_Rectangle.Y, 2, Size_Rectangle.Height), Color.Red);
                //middle lines
                //spriteBatch.Draw(pixel, new Rectangle(emitter.position_X, Size_Rectangle.Y, 2, Size_Rectangle.Height), Color.Red);
                //spriteBatch.Draw(pixel, new Rectangle(Size_Rectangle.X, emitter.position_Y, Size_Rectangle.Width, 2), Color.Red);
            }

            //spriteBatch.DrawString(Font, Message, Vector2.Zero, Color.White);
        }

        public void updateSizeRect()
        {
            emitter.setSizeRectangle();
            Size_Rectangle.X = emitter.Size_X;
            Size_Rectangle.Y = emitter.Size_Y;
            Size_Rectangle.Width = emitter.Width;
            Size_Rectangle.Height = emitter.Height;
        }

        public void updateEmitterPosition(int width, int height)
        {
            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;
            graphics.ApplyChanges();
            emitter.setStartUpPosition(width, height);
            updateSizeRect();

        }

    }
}
